﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Sistema
{
    public partial class FrmCadAgen : Form
    {
        public FrmCadAgen()
        {
            InitializeComponent();
            Fill_Combo();
        }

        void Fill_Combo()
        {

            Conexao.Conn.Open();
            string sql = "select nome from mydb.cliente";
            MySqlCommand cmd = new MySqlCommand(sql, Conexao.Conn);
            MySqlDataReader rs;


            try
            {

                
                rs = cmd.ExecuteReader();

                while (rs.Read())
                {
                    string name = rs.GetString("nome");
                    comboNome.Items.Add(name);
                }





            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.Message);

            }
            Conexao.Conn.Close();


        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboNome_SelectedIndexChanged(object sender, EventArgs e)
        {
            Conexao.Conn.Open();
            string sql = "select * from mydb.cliente where nome='"+comboNome.Text+"'";
            MySqlCommand cmd = new MySqlCommand(sql, Conexao.Conn);
            MySqlDataReader rs;


            try
            {

                
                rs = cmd.ExecuteReader();

	            while(rs.Read())
	            {
		        string codigo = rs.GetInt32("codigo_cl").ToString();
		        txtcliente.Text = codigo;
                }

            } 
            catch(MySqlException ee)
            {
            MessageBox.Show(ee.Message);

            }
            Conexao.Conn.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

            Conexao.Conn.Open();
            MySqlCommand sql = new MySqlCommand("insert into mydb.agendamento(codigo_cliente,data_visita) values(" + this.txtcliente.Text + ",'" + this.dateTimePicker1.Text + "')", Conexao.Conn);
            sql.ExecuteNonQuery();
            Conexao.Conn.Close();

            MessageBox.Show("Visita agendada com sucesso!");

            }
            catch(Exception ex)
            {
            MessageBox.Show(ex.Message);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new FrmConsAgen().ShowDialog();
        }

    
    }
}
